Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for access EJB session Bean into Servlet
Previous Home Next

This Servlet application, accesses EJB session Bean class into servlet. Remember -Tomcat web server doesn't support EJB yet for this program use GlassFish server.Consider following step for create this application-

Step-1 Create a new web project and name it as EJBServlet. Go to source package and name it as r4r.EJB, now right click over package and go to New -> other ->New File, window open (as show into below picture). Now choose Enterprise JavaBeans from the Categories list and Session Bean from File Types list, then click on Next button.

Step-2 Now New Session Bean window open. Name it and select session Type (in this program stateless use). After fill all the entry clickon Finish button.

Application directory structure

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <servlet>
  <servlet-name>EJBServlet</servlet-name>
  <servlet-class>r4r.EJB.EJBServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>EJBServlet</servlet-name>
  <url-pattern>/EJBServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
    30
  </session-timeout>
 </session-config>
</web-app>

index.jsp

<%-- 
 Document: index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
            content="text/html; charset=UTF-8">
  <title>r4r.co.in-index</title>
 </head>
 <body>
  <h1>EJB in Servelt!</h1> 
  <form action="EJBServlet" method="POST">
   Set title:  <input type="text" name="title"
                   value="" size="30" /><br/>
   Description: <textarea name="description" 
               rows="4" cols="30"></textarea><br/>
   Priority: <select name="priority">
  <option> Low </option>
  <option> MEDIUM </option>
  <option> HIGH </option>
  </select><br/>
  Create on: <input type="text" name="date"
                        value="" size="20" /><br/>
  <input type="submit" value="Add to TODO list" />
  <input type="reset" value="Reset" />
  </form>
 </body>
</html>

Servlet Program

/*
 * Save as a EJBServlet.java
 */
package r4r.EJB;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class EJBServlet extends HttpServlet
{
//Indicates a dependency on the no-interface Enterprise Java Bean
 @javax.ejb.EJB
 private EJBSessionBean sessionBean;
 @Override
 protected void doGet(HttpServletRequest request,
  HttpServletResponse response throws ServletException,IOException 
{
  //doGet role play in index.jsp page
 }
 @Override
 protected void doPost(HttpServletRequest request,
  HttpServletResponse response)throws ServletException,IOException
{
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
//get textField value from index.jsp page
String title = request.getParameter("title");
String description = request.getParameter("description");
String priority = request.getParameter("priority");
String date = request.getParameter("date");

// set data into EJBSessionBean class
sessionBean = new EJBSessionBean
	  (title, description, priority, date);

//Generate HTML page
out.println("<html><head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head><body>");
out.println("<h1>ToDo Data list</h1>");

//get data form EJBSession bean
out.println("Title : " + " <b> "
            + sessionBean.getTitle() + "</b><br/>");
out.println("Description  : " + " <b> " 
	        + sessionBean.getDescription() + "</b><br/>");
out.println("Priority : " + " <b> " 
	        + sessionBean.getPriority() + "</b><br/>");
out.println("Create Date  : " + "<b> "
            + sessionBean.getDate() + "</b><br/>");
out.println("</body></head>");
out.println("<a href=\"index.jsp\">Add more todo into list</a>");
  } finally {
out.flush(); //free resource
out.close();
  }
 }

 @Override
 public String getServletInfo() {
  return "r4r.co.in-EJBServlet";
 }
}

EJBSessionBean program

 * Save as a EJBSessionBean.java
 */
package r4r.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
 *
 * @author R4R
 */
@Stateless
@LocalBean
public class EJBSessionBean {

 public String title;
 public String description;
 public String priority;
 public String date;

 // default constructor
 public EJBSessionBean() {
 }

 // full constructor
 public EJBSessionBean
 (String title, String description, String priority, String date) 
 {
  this.title = title;
  this.description = description;
  this.priority = priority;
  this.date = date;
 }

 // getter/setter
 public String getDate() {
  return date;
 }

 public void setDate(String date) {
  this.date = date;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getPriority() {
  return priority;
 }

 public void setPriority(String priority) {
  this.priority = priority;
 }
}
Output of Program
Previous Home Next